/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package livret.controleur;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import livret.modele.*;

/**
 * C'est une classe qui intéragit avec la base de données
 * Elle récupères les données
 * Elle mit à jour les données
 * Elle traite les données et elle les affiche dans les vues
 * @author Solohery RAMANANARIVO, Fawzia CHABOUNI, Luc RABARIJAONA
 */
public class Controleur extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String forward ="";
        String action = request.getParameter("action");
        try {
            if (action.equals("authEtudiant")) {
                System.out.println("authEtudiant");
                forward = authentifierEtudiant(request);
            } else 
                if (action.equals("deconnexion")){
                    System.out.println("deconnexion");
                    request.getSession().invalidate();
                    forward = "Accueil.jsp";
                }
                else if (action.equals("profilEtudiant")) {
                    System.out.println("profilEtudiant");
                    forward = "Profil_etudiant.jsp";
                }
                else if (action.equals("coursEtudiant")) {
                     System.out.println("coursEtudiant");
                     forward =  "Liste_cours_etudiant.jsp";
                }
                else if (action.equals("coursCommentairesEtudiant")){
                     System.out.println("commentaireEtudiant");
                     forward = afficheCoursCommentaireEtudiant(request);
                }
                else if (action.equals("retourAccueilEtudiant")){
                     System.out.println("retourAccueilEtudiant");
                     forward = "Etudiant_view.jsp";
                }
                else if (action.equals("posterCommentaire")){
                     System.out.println("posterCommentaire");
                     forward = ajouterCommentaireEtudiant(request);
                }
                else if (action.equals("authFormateur")){
                    System.out.println("authFormateur");
                    forward = authentifierFormateur(request);
                }
                else if (action.equals("retourAccueilFormateur")){
                    System.out.println("retourAccueilFormateur");
                    forward = "Formateur_view.jsp";
                }
                else if (action.equals("profilFormateur")){
                    System.out.println("profilFormateur");
                    forward = "Profil_formateur.jsp";
                }
                else if (action.equals("coursFormateur")){
                    System.out.println("coursFormateur");
                    forward = "Liste_cours_formateur.jsp";
                }
                else if (action.equals("etudiantTuteur")){
                    System.out.println("etudiantTuteur");
                    forward = "Liste_etudiant_tuteur.jsp";
                }
                else if (action.equals("commentairesTuteur")){
                    System.out.println("commentairesTuteur");
                    forward = afficheCommentairesTuteur(request);
                }
                else if (action.equals("posterCommentaireFormateur")){
                    System.out.println("posterCommentaireFormateur");
                    forward = ajouterCommentaireFormateur(request);
                }
                else if (action.equals("coursCommentairesFormateur")){
                    System.out.print("coursCommentairesFormateur");
                    forward = afficheCoursCommentaireFormateur(request);
                }
                else
                    forward = "Error.jsp";
          }
         finally {
            RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
            dispatcher.include(request, response);
            out.close();
        }
    } 


    private String authentifierEtudiant(HttpServletRequest request) throws SQLException {
        String forward;
        String login = request.getParameter("id_etudiant");
        String pass = request.getParameter("password");
        Etudiant etudiant = Etudiant.getByLoginPassword(login, pass);
        if (etudiant != null) {
            forward = "Etudiant_view.jsp";
            HttpSession session = request.getSession();
            session.setAttribute("etudiant",etudiant);
        } else {
            forward = "Error.jsp";
            request.setAttribute("erreur","Erreur d'authentification étudiant");
         }
        return forward;
    }

    private String authentifierFormateur(HttpServletRequest request) throws SQLException {
        String forward;
        String login = request.getParameter("id_formateur");
        String pass = request.getParameter("password");
        Formateur formateur = Formateur.getByLoginPassword(login, pass);
        if (formateur != null) {
            forward = "Formateur_view.jsp";
            HttpSession session = request.getSession();
            session.setAttribute("formateur",formateur);
        } else {
            forward = "Error.jsp";
            request.setAttribute("erreur","Erreur d'authentification formateur");
         }
        return forward;
    }

    private String authentifierMaitreStage(HttpServletRequest request) throws SQLException {
        String forward;
        String login = request.getParameter("id_maitrestage");
        String pass = request.getParameter("password");
        MaitreStage maitrestage = MaitreStage.getByLoginPassword(login, pass);

        if (maitrestage != null) {
            forward = "MaitreStage_view.jsp";
            HttpSession session = request.getSession();
            session.setAttribute("maitrestage",maitrestage);
        } else {
            forward = "Error.jsp";
            request.setAttribute("erreur","Erreur d'authentification maître de stage");
         }
        return forward;
    }

    private String authentifierAdministration(HttpServletRequest request) throws SQLException {
        String forward;
        String login = request.getParameter("id_administration");
        String pass = request.getParameter("password");
        //Administration administration = Administration.

        //if (maitrestage != null) {
            forward = "Administration_view.jsp";
            HttpSession session = request.getSession();
          //  session.setAttribute("maitrestage",maitrestage);
        /*} else {
            forward = "Error.jsp";
            request.setAttribute("erreur","Erreur d'authentification maître de stage");
         }*/
        return forward;
    }

    private String afficheCoursCommentaireEtudiant(HttpServletRequest request) throws SQLException{
        int id_cours = Integer.parseInt(request.getParameter("id_cours"));
        int id_dest = Integer.parseInt(request.getParameter("id_dest"));
        HttpSession session = request.getSession();
        session.setAttribute("id_cours",id_cours);
        String forward="Cours_commentaires_etudiant.jsp";
        return forward;  
    }

    private String afficheCoursCommentaireFormateur(HttpServletRequest request) throws SQLException{
        int id_cours = Integer.parseInt(request.getParameter("id_cours"));
        HttpSession session = request.getSession();
        session.setAttribute("id_cours",id_cours);
        String forward="Liste_etudiant_formateur.jsp";
        return forward;
    }

    private String afficheCommentairesTuteur(HttpServletRequest request) throws SQLException{
        int id_cours = Integer.parseInt(request.getParameter("id_cours"));
        int id_dest = Integer.parseInt(request.getParameter("id_etudiant"));
        HttpSession session = request.getSession();
        session.setAttribute("id_cours",id_cours);
        session.setAttribute("id_dest",id_dest);
        String forward="Cours_commentaires_formateur.jsp";
        return forward;
    }

    private String ajouterCommentaireEtudiant(HttpServletRequest request) throws SQLException{
        int id_cours = Integer.parseInt(request.getParameter("id_cours"));
        int id_dest = Integer.parseInt(request.getParameter("id_dest"));
        String date = request.getParameter("date");
        String contenu = request.getParameter("contenu");
        HttpSession session = request.getSession();
        Etudiant etudiant =(Etudiant)session.getAttribute("etudiant");
        etudiant.PosterCommentaireCours(date, contenu, id_dest, id_cours);
        ArrayList<Commentaire> commentaires = etudiant.getListeCommentairesCours(id_cours);
        session.setAttribute("commentaires",commentaires);
        session.setAttribute("id_cours",id_cours);
        String forward="Cours_commentaires_etudiant.jsp";
        return forward;
    }

    private String ajouterCommentaireFormateur(HttpServletRequest request) throws SQLException{
        int id_cours = Integer.parseInt(request.getParameter("id_cours"));
        int id_dest = Integer.parseInt(request.getParameter("id_dest"));
        String date = request.getParameter("date");
        String contenu = request.getParameter("contenu");
        HttpSession session = request.getSession();
        Formateur formateur = (Formateur)session.getAttribute("formateur");
        formateur.PosterCommentaire(date, contenu, id_dest, id_cours);
        ArrayList<Commentaire> commentaires = formateur.getListeCommentairesCours(id_dest, id_cours);
        session.setAttribute("commentaires",commentaires);
        session.setAttribute("id_cours",id_cours);
        String forward="Cours_commentaires_formateur.jsp";
        return forward;
    }

    private String ajouterCommentaireMaitreStage(HttpServletRequest request) throws SQLException{
        String forward="";

        return forward;
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Controleur.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Controleur.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
